// 测试热门服务功能
const cloud = require('wx-server-sdk')

cloud.init({
  env: 'prod-4g9qmk2w95c15c9e' // 你的环境ID
})

async function testHotServices() {
  console.log('🔥 开始测试热门服务功能...\n')

  try {
    // 1. 测试获取热门服务
    console.log('1️⃣ 测试获取热门服务...')
    const hotServicesResult = await cloud.callFunction({
      name: 'serviceManager',
      data: {
        action: 'getHotServices',
        limit: 6
      }
    })

    if (hotServicesResult.result.success) {
      console.log('✅ 获取热门服务成功')
      console.log('热门服务数量:', hotServicesResult.result.data.length)
      hotServicesResult.result.data.forEach((service, index) => {
        console.log(`  ${index + 1}. ${service.title} (评分: ${service.rating}, 分类: ${service.categoryInfo.name})`)
      })
    } else {
      console.log('❌ 获取热门服务失败:', hotServicesResult.result.error)
    }

    console.log('\n' + '='.repeat(50) + '\n')

    // 2. 测试获取所有服务（检查isHot字段）
    console.log('2️⃣ 检查服务的isHot字段...')
    const allServicesResult = await cloud.callFunction({
      name: 'serviceManager',
      data: {
        action: 'getAllServices'
      }
    })

    if (allServicesResult.result.success) {
      console.log('✅ 获取所有服务成功')
      const hotServices = allServicesResult.result.data.filter(s => s.isHot === true)
      const nonHotServices = allServicesResult.result.data.filter(s => s.isHot === false)
      const undefinedHotServices = allServicesResult.result.data.filter(s => s.isHot === undefined)

      console.log(`总服务数: ${allServicesResult.result.data.length}`)
      console.log(`标记为热门的服务: ${hotServices.length}`)
      console.log(`标记为非热门的服务: ${nonHotServices.length}`)
      console.log(`未设置isHot字段的服务: ${undefinedHotServices.length}`)

      if (hotServices.length > 0) {
        console.log('\n标记为热门的服务:')
        hotServices.forEach((service, index) => {
          console.log(`  ${index + 1}. ${service.title} (${service.category}, 评分: ${service.rating})`)
        })
      }

      if (undefinedHotServices.length > 0) {
        console.log('\n⚠️  发现未设置isHot字段的服务:')
        undefinedHotServices.forEach((service, index) => {
          console.log(`  ${index + 1}. ${service.title} (${service.category})`)
        })
      }
    } else {
      console.log('❌ 获取所有服务失败:', allServicesResult.result.error)
    }

    console.log('\n' + '='.repeat(50) + '\n')

    // 3. 测试更新单个服务的热门状态
    if (allServicesResult.result.success && allServicesResult.result.data.length > 0) {
      console.log('3️⃣ 测试更新单个服务的热门状态...')
      
      // 找一个非热门的服务来测试
      const nonHotService = allServicesResult.result.data.find(s => s.isHot === false)
      
      if (nonHotService) {
        console.log(`选择服务: ${nonHotService.title} (当前状态: 非热门)`)
        
        // 设置为热门
        const updateResult = await cloud.callFunction({
          name: 'serviceManager',
          data: {
            action: 'updateServiceHotStatus',
            serviceId: nonHotService._id,
            isHot: true
          }
        })

        if (updateResult.result.success) {
          console.log('✅ 设置为热门成功:', updateResult.result.message)
          
          // 再设置回非热门
          const revertResult = await cloud.callFunction({
            name: 'serviceManager',
            data: {
              action: 'updateServiceHotStatus',
              serviceId: nonHotService._id,
              isHot: false
            }
          })

          if (revertResult.result.success) {
            console.log('✅ 恢复为非热门成功:', revertResult.result.message)
          } else {
            console.log('❌ 恢复为非热门失败:', revertResult.result.error)
          }
        } else {
          console.log('❌ 设置为热门失败:', updateResult.result.error)
        }
      } else {
        console.log('⚠️  没有找到非热门服务用于测试')
      }
    }

    console.log('\n' + '='.repeat(50) + '\n')

    // 4. 测试批量更新热门状态
    if (allServicesResult.result.success && allServicesResult.result.data.length >= 2) {
      console.log('4️⃣ 测试批量更新热门状态...')
      
      const testServices = allServicesResult.result.data.slice(0, 2)
      const updates = testServices.map(service => ({
        serviceId: service._id,
        isHot: !service.isHot // 切换状态
      }))

      console.log('批量更新操作:')
      updates.forEach((update, index) => {
        const service = testServices[index]
        console.log(`  ${service.title}: ${service.isHot} → ${update.isHot}`)
      })

      const batchUpdateResult = await cloud.callFunction({
        name: 'serviceManager',
        data: {
          action: 'batchUpdateHotStatus',
          updates: updates
        }
      })

      if (batchUpdateResult.result.success) {
        console.log('✅ 批量更新成功')
        console.log('更新结果:', batchUpdateResult.result.data.summary)
        
        // 恢复原状态
        const revertUpdates = testServices.map(service => ({
          serviceId: service._id,
          isHot: service.isHot // 恢复原状态
        }))

        const revertResult = await cloud.callFunction({
          name: 'serviceManager',
          data: {
            action: 'batchUpdateHotStatus',
            updates: revertUpdates
          }
        })

        if (revertResult.result.success) {
          console.log('✅ 恢复原状态成功')
        }
      } else {
        console.log('❌ 批量更新失败:', batchUpdateResult.result.error)
      }
    }

    console.log('\n🎉 热门服务功能测试完成!')

  } catch (error) {
    console.error('❌ 测试过程中发生错误:', error)
  }
}

// 运行测试
testHotServices() 